<?php
namespace app\user\controller;
use think\Session;
use think\Controller;
use think\Request;
use Nette\Mail\Message;
use Nette\Mail\SendmailMailer;
use app\common\traits\controller\TraitController;
/**
 * 
 */
class Intergrate extends Controller
{
    use TraitController;

    //前置操作
    protected $beforeActionList = [
        'checkLogin',
        'checkCaravanStatus'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
    /**
     * [checkCaravanStatus 房车是否审核通过]
     * @return [type] [description]
     */
    protected function checkCaravanStatus()
    {
        //1=审核通过
        $status = model('CaravanInfos')->where('user_id',session('user.id'))->value('status');
        if ($status<>1) {
            $this->error('房车未审核通过','/caravaninfo/index');
            
        }
    }
    
    public function getIndex()
    {   
        $integral = model('Intergrates')->getUserIntegral();
        $this->assign('integral',$integral);
        $list = model('Intergrates')->getIntergratesLists();//halt($list);
        $this->assign('list',$list);
        $incomelist = model('Intergrates')->getIncomeIntergratesLists();//halt($list);
        $this->assign('incomelist',$incomelist);
        $spendlist = model('Intergrates')->getSpendIntergratesLists();//halt($list);
        $this->assign('spendlist',$spendlist);
        return view('intergrate/index');
    }
   
    

    public function getIntergrateAdd()
    {
        $user_id = session('user.id');
        $mobile = model('Users')->where('id',$user_id)->value('mobile');
        $this->assign('mobile',$mobile);
        return view('intergrate/intergrateadd');
    }

    public function postSaveIntergrate()
    {
        $intergrate=input('post.intergrate');
        $currency=model('Intergrates')->integralToMoney($intergrate, 'CNY');//halt($currency);
        return $currency;
    }

    public function getPayChose()
    {
        return view('intergrate/paychose');
    }

    public function postSaveIntergrateAdd()
    {
        $paychose =input('post.paychose');
        $intergrateadd_info['intergrate'] = input('post.intergrate');
        $intergrateadd_info['cash'] = $this->postSaveIntergrate();//halt($cash);
        $intergrateadd_info['order_num'] = model('Orders')->createOrderId();
        $intergrateadd_info['user_id'] = session('user.id');
        $intergrateadd_info['status'] = 0;//halt($intergrateadd_info);
        $count = model('Intergrateadds')->createIntergrateadd($intergrateadd_info);
        if ($count) {
            $param  = [
                'order_id' => urlencode($count->order_num),
                'pt'       => urlencode('wx_pay'),
                'type'     => urlencode('recharge'),
                'body'     => urlencode('充值订单支付'),
                'detail'   => urlencode($count->toJson()),
                'total'    => urlencode($count->cash)
            ];
            $sign  = $this->mixSign($param);
            $param = array_merge($param, ['sign' => $sign]);
            queue('common/JobPay@recharge', $count->order_num, 0, 'order');
            
            $this->redirect(url('/wxpay/dopay', $param, true, 'wx'));
        }


        
        // $a=input('post.mobile');
        // $list=model('Users')->where('mobile','=',$a)->find();
        // $id=$list['id'];
        
        // $remark='积分充值';
        // $intergrateadd=model('Intergrates')->addIntergrates($intergrate,$id,$remark);
        // if ($intergrateadd) {
        //     return $this->success('积分增加成功','/intergrate/index');
        // }else{
        //     return $this->error('积分增加失败');
        // }

        
    }


    
}

